/**
* Created by wust on 2019/5/14.
*/
<template xmlns:v-data-dictionary-directive="http://www.w3.org/1999/xhtml">
  <el-form ref="formModel" :model="formModel" :rules="rules" label-width="130px" style="width: 70%;" @submit.native.prevent>
    <el-form-item label="登录账号" prop="loginName">
      <el-input v-model="formModel.loginName"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="realName">
      <el-input v-model="formModel.realName"></el-input>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="formModel.email"></el-input>
    </el-form-item>
    <el-form-item label="性别">
      <select class="form-control" v-data-dictionary-directive:A1003  v-model="formModel.sex">
      </select>
    </el-form-item>
    <el-form-item label="联系电话">
      <el-input v-model="formModel.mobilePhone"></el-input>
    </el-form-item>
    <el-form-item label="账号种类" prop="type">
      <select class="form-control" v-data-dictionary-directive:A1004 v-model="formModel.type">
      </select>
    </el-form-item>
    <el-form-item style="text-align: left;">
      <el-button type="primary" @click="onSubmit('formModel')" :loading="submitting">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import Vue from 'vue'

export default {
  name: 'user-create',
  data () {
    return {
      submitting: false,
      formModel: {
        loginName: '',
        realName: '',
        sex: '',
        email: '',
        type: '',
        mobilePhone: '',
        status: ''
      },
      rules: {
        loginName: [{required: true, message: '请输入登录账号', trigger: 'blur'}],
        realName: [{required: true, message: '请输入真实姓名', trigger: 'blur'}],
        type: [{required: true, message: '请选择账号类型', trigger: 'blur'}]
      }
    }
  },
  methods: {
    onSubmit: function (formData) {
      this.$refs[formData].validate((valid) => {
        if (!valid) {
        } else {
          this.submitting = true
          Vue.$ajax({
            method: 'post',
            url: Vue.$adminServerURL + '/web/v1/UserController',
            data: this.formModel
          }).then(res => {
            this.submitting = false
            if (res.data.flag !== 'SUCCESS') {
              if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                })
              }
            } else {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.resetForm('formModel')
            }
          })
        }
      })
    },
    resetForm (formData) {
      this.$refs[formData].resetFields()
    }
  }
}
</script>
